package com.sina.shop.api;

import com.sina.shop.domain.admin.Admin;
import com.sina.shop.service.admin.AdminService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-07-13:31
 */
@RequestMapping("/admin")
@RestController
public class AdminController extends BaseController {

    @Autowired
    @Qualifier("adminService")
    private AdminService adminService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getAdmin", method = RequestMethod.GET)
    public Admin getAdmin(@RequestParam(value = "account") String account) {

        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getAdmin, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        Admin admin = adminService.getAdminByAccount(account);

        return admin;
    }

    @RequestMapping(value = "/saveAdmin", method = RequestMethod.POST)
    public Admin saveAdmin(Admin admin) {

        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveAdmin, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        admin = adminService.saveAdmin(admin);

        return admin;
    }


    @RequestMapping(value = "/updateAdmin", method = RequestMethod.POST)
    public Admin updateAdmin(Admin admin) {

        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateAdmin, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        admin = adminService.updateAdmin(admin);

        return admin;
    }
}
